<?php
if(!defined('BASEPATH')) exit('No direct script access allowed');
/* 
	redis类
*/
class Myredis extends Redis{
	
	private $config = array(
		'type' => 'tcp',
		'socket' => null,
		'host' => '127.0.0.1',
		'auth' => 'LcMzyyNXm68G6z7S',
		'port' => 6379,
		'database' => 1,
		'timeout' => 300
	);
	
	public function __construct($config = array()){
		$this->initialize($config);
	}
	
	public function initialize($config){
		if(count($config) > 0){
			foreach($config as $key => $val){
				if(isset($this->config[$key])){
					$this->config[$key] = $val;
				}
			}
		}
		return $this->do_connect($this->config);
	}
	
	private function do_connect($config){
        if(isset($config['type']) && $config['type'] == 'unix'){
            if (!isset($config['socket'])) {
                return 'redis config key [socket] not found';
            }
            $this->connect($config['socket']);
        }else{
            $port = isset($config['port']) ? intval($config['port']) : 6379;
            $timeout = isset($config['timeout']) ? intval($config['timeout']) : 300;
            $this->connect($config['host'], $port, $timeout);
        }
        if(isset($config['auth']) && !empty($config['auth'])){
            $this->auth($config['auth']);
        }
        $this->db = isset($config['database']) ? intval($config['database']) : 0;
        $this->select($this->db);
        return $this;
    }
	
}